package controller;


import com.alibaba.fastjson.JSON;
import dao.CaseDao;
import dao.MakeDao;
import dao.ScheduleDao;
import entity.Case;
import entity.Nurse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.CaseService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/case")
@Scope("prototype")
public class CaseController {

    @Resource(name = "caseDao")
    private CaseDao caseDao;
    @Autowired
    private CaseService caseService;

    @Autowired
    private MakeDao makeDao;

    @Autowired
    private ScheduleDao scheduleDao;
    @RequestMapping("/getCaseById")
    @ResponseBody
    public List<Case> getCaseById(int pid){
        List<Case> list = caseDao.getCaseById(pid);
        return list;
    }

    @RequestMapping("/getCase")
    @ResponseBody
    public Case getCase(int cid){
        Case ca = caseDao.getCase(cid);
        return ca;
    }

    @RequestMapping("/query")
    @ResponseBody
    public Map<String,Object> query(Case c, @RequestParam(value = "pageNumber",defaultValue = "1",required = false) int pageNumber, @RequestParam(value = "pageSize",defaultValue = "5",required = false) int pageSize){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("total",caseDao.getCount(c));
        map.put("rows",caseService.query(c,pageSize,pageNumber));
        return map;
    }
    @RequestMapping("/toAddCase")
    @ResponseBody
    boolean toAddCase(Case c,String starTime,String endTime,String scheduleid,HttpServletRequest request){
        Map<String,Object> map=new HashMap<String, Object>();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String date= format.format(new Date());
        map.put("nId",c.getnId());
        map.put("star",starTime);
        map.put("end",endTime);
        map.put("rDate",new Date());
        map.put("sid",scheduleid);
        Map<String,Object> map1=caseDao.getPid(map);
        if(map1!=null){
            map1.put("sid",scheduleid);
            request.getSession().setAttribute("p",map1);
            int mid=(int) map1.get("m_id");
            Map<String,Object> map2=new HashMap<String, Object>();
            map2.put("mid",mid);
            map2.put("firstDate",date);
            if(makeDao.UpDateByStat(map2)>0 &&scheduleDao.UpDateByStat(scheduleid,"2")>0){
                System.out.println(1);
                return true;
            }else{
                return  false;
            }
        }
        return false;
    }

    @RequestMapping("/AddCase")
    @ResponseBody
    boolean AddCase(Case c,String prescription,String sid,String mid,String money){
        return caseService.AddCase(c,sid,prescription,mid,money);
    }

    @RequestMapping("/HalfYear")
    @ResponseBody
    public Map<String,Object> HalfYear(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("HalfYear",caseDao.HalfYear());
        return map;
    }


}
